//
//  ViewController.m
//  UI05_简单的小相册功能
//
//  Created by Hanlingyu on 16/7/20.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"

#define KWidth self.view.frame.size.width
#define KHeight self.view.frame.size.height

@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSArray *imageArray = @[@"guidePage_01", @"guidePage_02", @"guidePage_03", @"guidePage_04"];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView.backgroundColor = [UIColor cyanColor];
    self.scrollView.contentSize = CGSizeMake((imageArray.count + 2) * KWidth, 0);
    self.scrollView.contentOffset = CGPointMake(KWidth, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.tag = 1;
    
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    for (NSInteger i = 1; i <= imageArray.count; i++) {
        
        UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(i * KWidth, 0, KWidth, KHeight)];
        smallScrollView.delegate = self;
        smallScrollView.minimumZoomScale = 1;
        smallScrollView.maximumZoomScale = 2;
        [self.scrollView addSubview:smallScrollView];
        smallScrollView.tag = smallScrollView.contentOffset.x / KWidth;
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
        imageView.backgroundColor = [UIColor redColor];
        imageView.image = [UIImage imageNamed:imageArray[i - 1]];
        [smallScrollView addSubview:imageView];
    }
    
    
    UIScrollView *firstSmallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0 * KWidth, 0, KWidth, KHeight)];
    firstSmallScrollView.delegate = self;
    firstSmallScrollView.minimumZoomScale = 1;
    firstSmallScrollView.maximumZoomScale = 2;
    firstSmallScrollView.tag = firstSmallScrollView.contentOffset.x / KWidth;
    [self.scrollView addSubview:firstSmallScrollView];
    
    UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
    firstImageView.backgroundColor = [UIColor redColor];
    firstImageView.image = [UIImage imageNamed:imageArray[imageArray.count - 1]];
    [firstSmallScrollView addSubview:firstImageView];

    
    UIScrollView *lastSmallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((imageArray.count + 1) * KWidth, 0, KWidth, KHeight)];
    lastSmallScrollView.delegate = self;
    lastSmallScrollView.minimumZoomScale = 1;
    lastSmallScrollView.maximumZoomScale = 2;
    lastSmallScrollView.tag = lastSmallScrollView.contentOffset.x / KWidth;
    [self.scrollView addSubview:lastSmallScrollView];
    
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
    lastImageView.backgroundColor = [UIColor redColor];
    lastImageView.image = [UIImage imageNamed:imageArray[0]];
    [lastSmallScrollView addSubview:lastImageView];

    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, KWidth / 3, 20)];
    pageControl.center = CGPointMake(KWidth / 2, KHeight / 7 * 6);
    pageControl.backgroundColor = [UIColor colorWithRed:0.627 green:0.642 blue:0.632 alpha:1.000];
    pageControl.layer.cornerRadius = 10;
    pageControl.numberOfPages = 4;
    pageControl.tag = 1100;
    [self.view addSubview:pageControl];
    [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
}

- (void)changePage:(UIPageControl *)pageControl {
    UIScrollView *scrollView = [self.view viewWithTag:1];
//    [UIView animateWithDuration:0.3 animations:^{
//        scrollView.contentOffset = CGPointMake(pageControl.currentPage * KWidth + KWidth, 0);
//    }];
    [scrollView setContentOffset:CGPointMake(pageControl.currentPage * KWidth + KWidth, 0) animated:YES];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    scrollView = [self.view viewWithTag:1];
    if (self.scrollView.contentOffset.x == KWidth * 5) {
        self.scrollView.contentOffset = CGPointMake(KWidth * 1, 0);
    }
    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(KWidth * 4, 0);
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    UIPageControl *pageControl = [self.view viewWithTag:1100];
    NSInteger number = (scrollView.contentOffset.x - KWidth) / KWidth;
    NSLog(@"%ld", number);
    pageControl.currentPage = number;
    
    
//    for (UIScrollView *smallScrollView in scrollView.subviews) {
//        smallScrollView.zoomScale = 1;
//        
//    }
    
    for (UIScrollView *tempScroll in self.scrollView.subviews) {
        if ([tempScroll isKindOfClass:[UIScrollView class]]) {
            tempScroll.zoomScale = 1.0;
        }
    }
    
    
    
}

//- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
//
//    scrollView.zoomScale = 1;
//
//}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
   
    return [scrollView.subviews firstObject];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
